package com.landeb.shop.huo.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.bean.BeanStatusInfo;
import com.landeb.shop.huo.httpRequest.UserConfirmationRequest;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PasswordChangeActivity extends BaseActivity implements View.OnClickListener {

    private Button btnUpdate;
    private LinearLayout btnBack;
    private EditText etOldPassword, etNewPassword, etConfirmPassword;

    private String mUrl;
    private Retrofit mRetrofit;
    private UserConfirmationRequest mRequest;

    public static void toAct(Context context) {
        Intent intent = new Intent(context, PasswordChangeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_change);

        OkHttpClient client = MyApplication.getClient();
        mUrl = MyApplication.BASE_URL;
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        mRequest = mRetrofit.create(UserConfirmationRequest.class);

        initView();
        initData();
        initEvent();

    }

    private void initEvent() {
        btnBack.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
    }

    private void initData() {
    }

    private void initView() {

        btnBack = (LinearLayout) findViewById(R.id.act_changePassword_btn_back);
        btnUpdate = (Button) findViewById(R.id.act_changePassword_btn_reset);

        etOldPassword = (EditText) findViewById(R.id.act_changePassword_et_oldPassword);
        etNewPassword = (EditText) findViewById(R.id.act_changePassword_et_newPassword);
        etConfirmPassword = (EditText) findViewById(R.id.act_changePassword_et_confirmPassword);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.act_changePassword_btn_back:
                // TODO: 2016/11/16 返回键
                onBackPressed();
                break;

            case R.id.act_changePassword_btn_reset:
                // TODO: 2016/11/16 修改密码


                if (etOldPassword.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordChangeActivity.this, "请输入旧密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (etNewPassword.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordChangeActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (etConfirmPassword.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordChangeActivity.this, "请确认新密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!etNewPassword.getText().toString().equals(etConfirmPassword.getText().toString())) {
                    Toast.makeText(PasswordChangeActivity.this, "输入密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                mRequest.changePassword(etOldPassword.getText().toString(), etNewPassword.getText().toString(), etConfirmPassword.getText().toString())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BeanStatusInfo>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BeanStatusInfo beanStatusInfo) {
                                Toast.makeText(PasswordChangeActivity.this, beanStatusInfo.getInfo(), Toast.LENGTH_SHORT).show();
                            }
                        });

                break;

        }

    }
}
